import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import Goodsadd from '../components/goods/Goodsadd'

Vue.use(VueRouter)
// 解决路由访问重复时报错问题：
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes : [
    {
      path: '/login', component: Login  },
    { path: '/', redirect: '/login' },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children:[
        {path:'/welcome',component: Welcome},
        {path: '/users', component: Users},
        {path:'/rights',component: Rights},
        {path:'/roles' , component: Roles},
        {path:'/categories',component:Cate},
        {path:'/params',component:Params},
        {path: '/goods',component: List},
        {path:'/goods/add',component: Goodsadd}]  //component t前面少写了一个n,造成组件无法匹配，不显示，而且没有错误提示！！！！！！！！！！！！！！！！！
    }

  ]
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to代表将要访问的路径
  //from代表从哪里跳转而来
  //next是一个函数,代表放行
  if(to.path=='/login') return next();
  var token=window.sessionStorage.getItem('token');
  if (!token) return next('/login');
  next();
})

export default router
